import Vue from 'vue'
import VueRouter from 'vue-router'
import {getCookie, setCookie} from "../utils/utils";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/storyList"
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login/index.vue')
    },

    {
        /*文章编辑器*/
        path: '/storyEdit',
        name: 'storyEdit',
        component: () => import("../views/story/storyEdit.vue")
    },
    {
        /*查询文章界面*/
        path: '/storyEditDailog',
        name: 'storyEditDailog',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/story/storyEditDailog.vue")
    },
    {
        /*查询文章列表*/
        path: '/storyList',
        name: 'storyList',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/story/list.vue")
    },
    {
        /*单纯显示文字*/
        path: '/storyOne',
        name: 'storyOne',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/story/one.vue")
    },
    {
        /*单纯显示文字*/
        path: '/storyTranslate',
        name: 'storyTranslate',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/story/translate.vue")
    },

    {
        /*自定义字段demo*/
        path: '/imageList',
        name: 'imageList',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/imageList/imageList.vue")
    },
    {
        path: '/ddl2json',
        name: 'ddl2json',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/ddl2json/ddl2json.vue")
    },
    {
        path: '/ddl2html',
        name: 'ddl2html',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/ddl2json/ddl2html.vue")
    },
    {
        path: '/ddl2form',
        name: 'ddl2form',
        // http://192.168.10.134:8080/storyEditDailog?uuid=6b5f2f7b-a228-414f-ab8d-2bb7578b2b44&puuid=1&title=1
        component: () => import("../views/ddl2json/ddl2form.vue")
    }


]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

function isLogin() {
    if (getCookie('currentUser')) {
        return true;
    } else {
        return false
    }
}

if (!isLogin()) {
    setCookie("currentUser", "")
}


//去掉没有登录 跳转首页
router.beforeEach((to, from, next) => {
    //拦截没有登录的时候的路由请求
    console.log("beforeEach to.path" ,to.path)
    if (to.path != '/login') {
        if (
            !isLogin()&&
            //白名单
            to.path != '/storyOne'&&
            to.path != '/ddl2json'
         ) {
            //还没有登录
            next("/login")
            return false;
        }
    }

    next();

})

export default router
